package com.saoft.jcms.web.config;

import com.saoft.jcms.web.dialect.SaoftDialect;
import com.saoft.jcms.web.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.templateresolver.UrlTemplateResolver;

@Configuration
public class WebMvcConfig {

    @Value("${jcms.upload-path}")
    private String filePath;

    @Bean
    SaoftDialect saoftDialect(){
        return new SaoftDialect();
    }

//    @Bean
//    public SpringTemplateEngine templateEngine() {
//        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
//        templateEngine.addTemplateResolver(new UrlTemplateResolver());
//        return templateEngine;
//    }


    @Bean
    public WebMvcConfigurer webMvcConfigurer() {

//        String filePath = "D:/fangcms-upload/";

        return new WebMvcConfigurer() {

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                registry
                        .addInterceptor(new LoginInterceptor())
//                        .excludePathPatterns("/","/index","search","/detail","/405")
                        .addPathPatterns("/archives/**")//拦截规则
//                        .excludePathPatterns("image/**","css/**","lib/**")//排除这些
                ;
            }

            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/uploads/**").addResourceLocations("file:" + filePath);
            }
        };
    }
}
